package mapEditor.utils;

import debug.*;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.Shape;
import java.awt.geom.GeneralPath;

import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import org.json.JSONArray;
import org.json.JSONException;

public class Util
{ private static final String TAG="mapEditor.utils.Util";
//  private static final boolean VERBOSE=true;
  private static final boolean DEBUG=false;//true;

	
  // Example: round(23.45678912345,100) ==> 23.46	
  public static double round(double x,int d)
  { double rounded=((double)Math.round(x*d))/d;
    //System.out.println("ROUND: "+x+"  ==>  "+rounded+", Math.round(x*d)="+Math.round(x*d));
	return(rounded);
  }
  
  public static void paint(Graphics2D g,Shape shape,FillStyle svgFill,StrokeStyle svgStroke)
  { if(shape==null) return;
	if(svgFill!=null) if(svgFill.getEnabled()) fill(g,shape,svgFill.getColor());
    if(svgStroke!=null) if(svgStroke.getEnabled()) draw(g,shape,svgStroke.getColor(),svgStroke.getBasicStroke());
  }
  
  public static void drawString(Graphics2D g,String str,float x,float y,double zoomScale,FontStyle svgFont,StrokeStyle svgStroke)
  { //if(color==null) return;
    //if(stroke==null) stroke=new BasicStroke(1);
	Font oldFont=g.getFont();
	Color oldColor=g.getColor();
    Stroke oldStroke=g.getStroke();
    g.setFont(svgFont.getFont(zoomScale));
    g.setColor(svgStroke.getColor());
    g.setStroke(svgStroke.getBasicStroke());
	g.drawString(str,x,y);
	g.setFont(oldFont); 
	g.setColor(oldColor); 
    g.setStroke(oldStroke);
  }
  
  public static void draw(Graphics2D g,Shape shape,Color color,Stroke stroke)
  { if(color==null) return;
    if(stroke==null) stroke=new BasicStroke(1);
	Color oldColor=g.getColor();
    Stroke oldStroke=g.getStroke();
    g.setColor(color);
    g.setStroke(stroke);
	g.draw(shape);
	g.setColor(oldColor); 
    g.setStroke(oldStroke);
  }

  public static void fill(Graphics2D g,Shape shape,Color color)
  { if(color==null) return;
	Color oldColor=g.getColor();
    g.setColor(color);
	g.fill(shape);
	g.setColor(oldColor); 
  }

  // ******************************************************************
  // *** METHOD: DrawLine
  // ******************************************************************
  public static void drawLine(Graphics2D g,MapLocation p,MapLocation q)
  { g.drawLine((int)p.getX(),(int)p.getY(),(int)q.getX(),(int)q.getY()); }


  public static Border getTitleBorder(String title)
  { Font boldFont=new Font("Dialog",Font.BOLD,12);
    Border border=new TitledBorder(null,title,TitledBorder.LEFT,TitledBorder.TOP,boldFont);
    return(border);
  }
  
  public static JSONArray encodeFloats(float[] f)
  { JSONArray arr=new JSONArray();
    if(f!=null) try
    { for(int i=0;i<f.length;i++) arr.put(f[i]); }
    catch(JSONException e) {}
    if(DEBUG) Log.d(TAG,"Util.encodeFloats: "+arr.toString());
    return(arr);
  }
  
  public static float[] decodeFloats(JSONArray arr)
  { int n=arr.length();
    if(n==0) return(null);
    try
    { float[] res=new float[n];
      for(int i=0;i<n;i++) res[i]=(float)arr.getDouble(i);
	  return(res);
    } catch(JSONException e) { return(null); }
  }

  public static Color alfaColor(Color color,int alfa)
  { if(color==null) return(null);
    int r=color.getRed();
	int g=color.getGreen();
	int b=color.getBlue();
	return(new Color(r,g,b,alfa));
  }
  
  public static JSONArray encodeRGBA(Color color)
  { JSONArray rgba=new JSONArray();
    if(color!=null)
    { rgba.put(color.getRed());
      rgba.put(color.getGreen());
      rgba.put(color.getBlue());
      rgba.put(color.getAlpha());
    }
    return(rgba);
  }
  
  public static Color decodeRGBA(String rgba)
  { try { return(decodeRGBA(new JSONArray(rgba))); }
    catch(JSONException e) { return(null); }
  }
  
  public static Color decodeRGBA(JSONArray rgba) throws JSONException
  { if(rgba==null) return(null);
    return(new Color(rgba.getInt(0),rgba.getInt(1),rgba.getInt(2),rgba.getInt(3)));
  }
  

  public static int fillRule(String rule)
  { if(rule.equalsIgnoreCase("nonzero")) return(GeneralPath.WIND_NON_ZERO);
    if(rule.equalsIgnoreCase("evenodd")) return(GeneralPath.WIND_EVEN_ODD);
    throw(new IllegalArgumentException("rule "+rule));
  }

  public static String fillRule(int rule)
  { switch(rule)
	{ case GeneralPath.WIND_NON_ZERO: return("nonzero");
	  case GeneralPath.WIND_EVEN_ODD: return("evenodd");
	  default: throw(new IllegalArgumentException("rule "+rule));
	}
  }


  public static int lineCap(String cap)
  { if(cap.equalsIgnoreCase("butt")) return(BasicStroke.CAP_BUTT);
    if(cap.equalsIgnoreCase("round")) return(BasicStroke.CAP_ROUND);
    if(cap.equalsIgnoreCase("square")) return(BasicStroke.CAP_SQUARE);
    throw(new IllegalArgumentException("cap "+cap));
  }

  public static String lineCap(int cap)
  { switch(cap)
	{ case BasicStroke.CAP_BUTT: return("butt");
	  case BasicStroke.CAP_ROUND: return("round");
	  case BasicStroke.CAP_SQUARE: return("square");
	  default: throw(new IllegalArgumentException("cap "+cap));
	}
  }

  public static int lineJoin(String join)
  { if(join.equalsIgnoreCase("miter")) return(BasicStroke.JOIN_MITER);
    if(join.equalsIgnoreCase("round")) return(BasicStroke.JOIN_ROUND);
    if(join.equalsIgnoreCase("bevel")) return(BasicStroke.JOIN_BEVEL);
    throw(new IllegalArgumentException("join "+join));
  }

  public static String lineJoin(int join)
  { switch(join)
	{ case BasicStroke.JOIN_MITER: return("miter");
	  case BasicStroke.JOIN_ROUND: return("round");
	  case BasicStroke.JOIN_BEVEL: return("bevel");
	  default: throw(new IllegalArgumentException("join "+join));
	}
  }

}
